import {ref} from 'vue';

function usePolling(func, interval) {
    const timer = ref(-1);

    function polling(...args) {
        clear();

        const callback = func.bind(this, args);
        callback();
        timer.value = setInterval(callback, interval);
    }

    function clear() {
        if (timer.value > 0) {
            clearInterval(timer.value);
            timer.value = -1;
        }
    }

    return {
        polling,
        clear,
    };
}

export default usePolling;
